package com.yonyou.crm.sys.dataright.handler;

import java.util.ArrayList;
import java.util.List;

import com.yonyou.crm.sys.dataright.entity.RightObject;
import com.yonyou.crm.sys.dataright.entity.SuperVO;

public class DeptHandler implements RightHandler {

	//权限实体信息
	RightObject rightObj;
	//当前部门
	Long curDept;
	
	public DeptHandler(RightObject rightObj, Long curDept) {
		super();
		this.rightObj = rightObj;
		this.curDept = curDept;
	}

	@Override
	public boolean hasRight(SuperVO vo) {
		return curDept == getDept(vo);
	}

	@Override
	public SuperVO[] filterWithRight(SuperVO[] vos) {
		if(vos == null || vos.length == 0) {
			return vos;
		}
		List<Object> result = new ArrayList<Object>();
		for(int i=0,len=vos.length;i<len;i++) {
			if(curDept == getDept(vos[i])) {
				result.add(vos[i]);
			}
		}
		return result.toArray(new SuperVO[0]);
	}

	@Override
	public String getRightSql() {
		return rightObj.getDeptField() + "= '" + curDept + "'";
	}

	private Long getDept(SuperVO vo) {
		return vo.getDeptId();
	}
}
